/* esp32管理开发板网址 */
/* https://dl.espressif.com/dl/package_esp32_index.json */

#include <WiFi.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>

const char* ssid     = "dudada666";
const char* password = "dududadaq";

const char* host = "api.seniverse.com";

void setup() {
    Serial.begin(115200);
    delay(10);

    /* We start by connecting to a WiFi network */
    Serial.println();
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);

    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
        delay(2500);
        Serial.print(".");
    }

    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
}

void loop() {
  // put your main code here, to run repeatedly:
  
  /* Use WiFiClient class to create TCP connections */
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }  
  
  /* We now create a URI for the request */
  String url = "/v3/weather/now.json?key=SRGsyCqT8sp1Y4gOF&location=guangzhou&language=en&unit=c";  
  
  /* This will send the request to the server */
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");
  delay(1000);  

  /* Read all the lines of the reply from server and print them to Serial */
  String answer;
  while(client.available()){
    String line = client.readStringUntil('\r');
    answer += line;
  }
  
//  Serial.println();
//  Serial.println("Answer: ");
//  Serial.println(answer);
  
  /* Disconnect to the server */
  client.stop();
  Serial.println();
  Serial.println("closing connection");

  /* Get Json data*/
  String jsonAnswer;
  int jsonIndex;
  for (int i = 0; i < answer.length(); i++) {
    if (answer[i] == '{') {
      jsonIndex = i;
      break;
    }
  }
  jsonAnswer = answer.substring(jsonIndex);
  Serial.println();
  Serial.println("JSON answer: ");
  Serial.println(jsonAnswer);

  const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(1) + 2*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(6) + 210;
  DynamicJsonDocument doc(capacity);
  
  //const char* json = "{\"results\":[{\"location\":{\"id\":\"WX4FBXXFKE4F\",\"name\":\"北京\",\"country\":\"CN\",\"path\":\"北京,北京,中国\",\"timezone\":\"Asia/Shanghai\",\"timezone_offset\":\"+08:00\"},\"now\":{\"text\":\"多云\",\"code\":\"4\",\"temperature\":\"5\"},\"last_update\":\"2020-11-19T19:00:00+08:00\"}]}";
  
  deserializeJson(doc, jsonAnswer);
  
  JsonObject results_0 = doc["results"][0];
  
  JsonObject results_0_location = results_0["location"];
  const char* results_0_location_id = results_0_location["id"]; // "WX4FBXXFKE4F"
  const char* results_0_location_name = results_0_location["name"]; // "北京"
  const char* results_0_location_country = results_0_location["country"]; // "CN"
  const char* results_0_location_path = results_0_location["path"]; // "北京,北京,中国"
  const char* results_0_location_timezone = results_0_location["timezone"]; // "Asia/Shanghai"
  const char* results_0_location_timezone_offset = results_0_location["timezone_offset"]; // "+08:00"
  
  JsonObject results_0_now = results_0["now"];
  const char* results_0_now_text = results_0_now["text"]; // "多云"
  const char* results_0_now_code = results_0_now["code"]; // "4"
  const char* results_0_now_temperature = results_0_now["temperature"]; // "5"
  
  const char* results_0_last_update = results_0["last_update"]; // "2020-11-19T19:00:00+08:00"

  Serial.print("city:name:");
  Serial.println(results_0_location_name);

  delay(5000);
}
